package day03;

import java.util.*;

public class PokerPerson {

    // 人员列表
    private Map<Integer, Person> map = new HashMap<>();

    // 暂存人员卡牌列表
    private List<Set<Card>> midList;

    // 排序后卡组
    private List<Card> newList;

    // 获胜人员
    private Set<Person> winSet;

    // 发牌
    private Poker pokerCard;

    // 注入
    public void setPokerCard(Poker pokerCard) {
        this.pokerCard = pokerCard;
    }

    public void initCard() {
        // 洗牌
        pokerCard.shufflePoker();
        // 获取牌
        newList = pokerCard.getNewList();
    }

    /**
     * 添加人员
     */
    public void addPerson(int num) {
        for (int i = 0; i < num; i++) {
            Scanner scanner = new Scanner(System.in);
            int index = i + 1;
            System.out.println("请输入玩家" + index + "姓名:");
            String name = scanner.next();
            System.out.println("请输入玩家" + index + "年龄:");
            int age = scanner.nextInt();
            map.put(i, new Person(index, age, name));
        }
    }

    /**
     * 改写：发牌
     *
     * @param num: 每人发牌数量
     */
    public void addCard(int num) {
        midList = new ArrayList<>(num);
        for (int i = num; i > 0; i--) {
            map.forEach((index, person) -> {
                System.out.println(person.getName() + " 拿牌");
                Card card = newList.get(new Random().nextInt(52));
                Set<Card> set = new TreeSet<>();
                set.add(card);
                if (midList.size() < map.size()) {
                    midList.add(set);
                } else {
                    Set<Card> cards = midList.get(index);
                    cards.add(card);
                    midList.set(index, cards);
                }
            });
        }
    }

    /**
     * 给每个人放入手牌
     */
    public void changePersonCard() {
        map.forEach((index, person) -> {
            person.setCardList(midList.get(index));
        });
    }

    /**
     * 比较手牌大小,并排序
     */
    public void checkPersonPoint() {
        winSet = new TreeSet<>();
        map.forEach((i, person) -> {
            Set<Card> cardList = person.getCardList();
            System.out.println(person.getName() + " 所有手牌为：" + cardList);
            Optional<Card> first = cardList.stream().sorted().findFirst();
            System.out.println("最大手牌为：" + first);
            first.ifPresent(card -> {
                person.setMaxCard(card);
                winSet.add(person);
            });
        });
    }

    /**
     * 获取胜利者
     */
    public void getWinPerson() {
        if (winSet.stream().findFirst().isPresent()) {
            Person person = winSet.stream().findFirst().get();
            System.out.println("最后胜者为：" + person.getName() + "，最大手牌为：" + person.getMaxCard());
        } else {
            System.out.println("平局");
        }
    }

    /**
     * 发牌
     *
     * @param num: 每人发牌数量
     */
    public void addCard2(int num) {
        map.forEach((index, person) -> {
            person.setCardList(getCardList(num));
        });
    }

    /**
     * 依次发牌
     */
    protected Set<Card> getCardList(int num) {
        Set<Card> set = new TreeSet<>();
        for (int i = num; i > 0; i--) {
            Card card = newList.get(new Random().nextInt(52));
            System.out.println(card);
            set.add(card);
        }
        return set;
    }

}
